/**
 * 
 */
package com.wolfpire.proms.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wolfpire.proms.dao.FeedBackDao;
import com.wolfpire.proms.dao.TaskDao;
import com.wolfpire.proms.dao.TaskPlanDao;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.TaskPlanService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.model.User;

/**
 * @author hj
 *
 */

@Service("taskPlanService")
public class TaskPlanServiceImpl extends BaseHibernateService<TaskPlan, Long>
		implements TaskPlanService {

	@Resource
	private TaskPlanDao taskPlanDao;

	@Resource
	private TaskDao taskDao;
	
	@Resource
	private FeedBackDao feedBackDao;

	@Override
	protected IBaseHibernateDao<TaskPlan, Long> getEntityDao() {
		return this.taskPlanDao;
	}

	@Override
	public Page<TaskPlan> setPageDataTaskPlanList(Page<TaskPlan> page,
			TaskPlan filterTaskPlan) {
		this.taskPlanDao.updateTimeoutFlag();
		Page<TaskPlan> pageD = this.taskPlanDao.findTasks(page, filterTaskPlan);

		List<TaskPlan> list = pageD.getDataList();

		for (TaskPlan taskPlan : list) {
			if (taskPlan.getTaskId() != null && taskPlan.getTaskId() > 0) {
				taskPlan.setTask(this.taskDao.get(taskPlan.getTaskId()));
			}
		}
		return pageD;
	}

	@Override
	public Integer countTaskPlanList(TaskPlan filterTaskPlan) {
		Integer count = this.taskPlanDao.countTaskPlanList(filterTaskPlan);
		return count;
	}

	@Override
	public void saveOrUpdateTaskPlan(TaskPlan taskPlan, User user) {
		if (null == taskPlan.getId()) {
			taskPlan.setCreateUserId(user.getId());
			taskPlan.setCreateUserName(user.getNickName());
			taskPlan.setStatus(0);
		} else {
			taskPlan.setUpdateTime(new Date());
			taskPlan.setUpdateUserId(user.getId());
			taskPlan.setUpdateUserName(user.getNickName());
		}
		// TODO : 修改时候默认不超时？这个逻辑会不会有问题？
		taskPlan.setIsTimeout(0);
		this.taskPlanDao.saveOrUpdate(taskPlan);

		planSaveAndTaskCallback(taskPlan);
	}

	@Override
	public TaskPlan getTaskPlan(Long id) {
		return this.taskPlanDao.get(id);
	}

	@Override
	public void delTaskPlan(Long id) {
		TaskPlan taskPlan = this.taskPlanDao.get(id);
		if (null != taskPlan.getTaskId()) {
			this.taskPlanDao.delete(id);
		} else {
			if (null != taskPlan) {
				taskPlan.setDelFlag(Constants.DELETE_FLAG);
				this.taskPlanDao.saveOrUpdate(taskPlan);
			}
		}
	}

	@Override
	public List<TaskPlan> list(TaskPlan entity) {
		return null;
	}

	protected void planSaveAndTaskCallback(TaskPlan taskPlan) {
		Long taskId = taskPlan.getTaskId();
		Task task = this.taskDao.get(taskPlan.getTaskId());
		// 任务进度取最后一个回写
		// String progress = this.feedBackDao.findLastProgressByTaskId(taskId);
		Map<String, Object> feedBackCount = this.feedBackDao
				.countWithPlan(taskId);
		BigDecimal donePlan = new BigDecimal(0);
		BigDecimal allPlan = new BigDecimal(0);
		if (null != feedBackCount.get("done_plan")) {
			donePlan = new BigDecimal(feedBackCount.get("done_plan").toString());
		}
		if (null != feedBackCount.get("all_plan")) {
			allPlan = new BigDecimal(feedBackCount.get("all_plan").toString());
		}
		Double progress = 0D;
		if (0 == allPlan.intValue()) {

		} else {
			progress = donePlan.divide(allPlan, 4, BigDecimal.ROUND_HALF_UP)
					.multiply(new BigDecimal(100)).doubleValue();
		}
		task.setProgress(progress);
		// 实际使用资金取总和回写
		String factAmount = this.feedBackDao.countFactAmountByTaskId(taskId);
		task.setFactAmount(factAmount);
		// 回填计划干系人
		if (taskPlan.getTaskId() != null) {

			if (task != null) {
				String actors = task.getTaskActorIds();
				String names = task.getTaskActorNames();
				if (actors != null && actors.length() > 0) {
					String[] actorsTemp = actors.split(",");
					String[] namesTemp = names.split(",");
					HashSet<String> actorsSet = new HashSet<String>();
					HashSet<String> namesSet = new HashSet<String>();
					for (int i = 0; i < actorsTemp.length; i++) {
						actorsSet.add(actorsTemp[i]);
						namesSet.add(namesTemp[i]);
					}
					actorsSet.add(taskPlan.getPlanActorId());
					namesSet.add(taskPlan.getPlanActorName());
					String _actors = "";
					String _names = "";
					int a_count = 0;
					int n_count = 0;
					for (String name : namesSet) {
						if (a_count > 0)
							_names += ",";
						_names += name;
						a_count++;
					}
					for (String actor : actorsSet) {
						if (n_count > 0)
							_actors += ",";
						_actors += actor;
						n_count++;
					}
					task.setTaskActorIds(_actors);
					task.setTaskActorNames(_names);
				}
			}
		}
		this.taskDao.saveOrUpdate(task);
	}

}
